package top.hkyzf.study.leetcode.tree;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-26 10:36
 */
public class 合并二叉树_617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        root1.val = root1.val + root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }

    @Test
    public void testMergeTrees () {
        TreeNode root1 = new TreeNode(1);
        TreeNode r1_node1 = new TreeNode(3);
        TreeNode r1_node2 = new TreeNode(2);
        TreeNode r1_node3 = new TreeNode(5);
        root1.left = r1_node1;
        root1.right = r1_node2;
        r1_node1.left= r1_node3;
        TreeNode root2 = new TreeNode(2);
        TreeNode r2_node1 = new TreeNode(1);
        TreeNode r2_node2 = new TreeNode(3);
        TreeNode r2_node3 = new TreeNode(4);
        TreeNode r2_node4 = new TreeNode(7);
        root2.left = r2_node1;
        root2.right = r2_node2;
        r2_node1.right = r2_node3;
        r2_node2.right = r2_node4;
        TreeNode treeNode = mergeTrees(root1, root2);
        treeNode.print();
    }
}
